package org.csu.hotelsys.service;

import org.csu.hotelsys.helpers.Constant;
import org.csu.hotelsys.model.Roomkt;
import org.csu.hotelsys.repository.RoomRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

import static org.csu.hotelsys.helpers.Constant.ROOM_STATUS.VERIFIED;

/**
 * @Transactional changes to model is reflected automatically in database
 */

@Service("roomServiceYH")
@Transactional
public class RoomServiceYH {

    @Autowired
    private RoomRepository roomRepository;

    public Roomkt findById(int id) {
        Optional<Roomkt> tmp = roomRepository.findById(id);
        return tmp.orElse(null);
    }

    public Roomkt findByName(String name) {
        return roomRepository.findByName(name).orElse(null);
    }

    public List<Roomkt> findByTypeId(int id) {
        return roomRepository.findByTypeIdAndStatus(id, VERIFIED);
    }

    public List<Roomkt> findByStatus(String status) {
        return roomRepository.findByStatus(status);
    }

    public void saveRoom(@NonNull Roomkt roomkt) {
        roomRepository.save(roomkt);
    }

    public void updateRoom(@NonNull Roomkt roomkt) {
        Optional<Roomkt> entity = roomRepository.findById(roomkt.getId());
        if (entity.isPresent()) {
            roomkt.setId(entity.get().getId());
            roomRepository.save(roomkt);
        }
    }

    public void deleteRoomById(int id) {
        roomRepository.deleteById(id);
    }

    public void deleteRoomByName(String name) {
        roomRepository.deleteByName(name);
    }

    public List<Roomkt> findAllRooms() {
        return roomRepository.findAll();
    }

    public boolean isRoomNameUnique(Integer id, String name) {
        Roomkt roomkt = findByName(name);
        return (roomkt == null || ((id != null) && (roomkt.getId().equals(id))));
    }

    public List<Roomkt> findFreeRooms() {
        return roomRepository.findFreeRooms(Constant.ROOM_STATUS.VERIFIED);
    }

    public List<Roomkt> findFreeRooms(Integer min, Integer max, String type) {
        if (type != null && type.toLowerCase().equals(Constant.ROOM_TYPE.FAMILY.toLowerCase())) {
            return roomRepository.findFreeRooms(min, max, Constant.ROOM_TYPE_VALUE.FAMILY, Constant.ROOM_STATUS.VERIFIED);
        } else if (type != null && type.toLowerCase().equals(Constant.ROOM_TYPE.DELUXE.toLowerCase())) {
            return roomRepository.findFreeRooms(min, max, Constant.ROOM_TYPE_VALUE.DELUXE, Constant.ROOM_STATUS.VERIFIED);
        } else if (type != null && type.toLowerCase().equals(Constant.ROOM_TYPE.EXECUTIVE.toLowerCase())) {
            return roomRepository.findFreeRooms(min, max, Constant.ROOM_TYPE_VALUE.EXECUTIVE, Constant.ROOM_STATUS.VERIFIED);
        }
        return roomRepository.findFreeRooms(min, max, Constant.ROOM_STATUS.VERIFIED);
    }

    public List<Roomkt> searchByName(String text) {
        return roomRepository.findByName(text, Constant.ROOM_STATUS.VERIFIED);
    }

}
